Test Failed
Push — master ( 0888ee...91d545 )
by Dmytro
02:36
created

Cottus   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 22
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A constructor 0 4 1
A addRules 0 3 2
A addRule 0 3 1
A compile 0 7 1
1
import { toArray } from 'myrmidon';
2
import BaseError from './errors/Base';
3
import ValidationError from './errors/ValidationError';
4
5
// const schema = [ 'boolean' ];
6
// const deep =  { 'object' : {
7
//     name            : [ 'string', { 'min': 3 } ],
8
//     password        : [ 'password' ],
9
//     confirmPassword : [ 'required', { 'eq': { '$ref': 'password' } } ]
10
// } };
11
12
// const array = [ 'array', { every: { object: {} } } ];
13
14
15
class Validator {
16
    constructor(cottus, schema) {
17
        this._schema = schema;
18
        this._cottus = cottus;
19
    }
20
21
    parse() {
22
        this._validators = [];
23
        toArray(this._schema).forEach(schema => {
24
            const Rule = this._cottus.rules[schema];
25
26
            this._validators.push(new Rule());
27
        });
28
    }
29
30
    // async function v() {
31
    //     const rule = new RequiredRule(params);
32
33
    //     try {
34
    //         rule.validate(input, context);
35
    //     } catch (error) {
36
    //         error.setContext(context); // check static
37
    //         throw error;
38
    //     }
39
    // }
40
41
42
    validate(input) {
43
        let valid = input;
44
        const errors = [];
45
46
        for (const validator of this._validators) {
47
            try {
48
                valid = validator.validate(valid);
49
            } catch (error) {
50
                if (!(error instanceof BaseError)) throw error;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
51
                error.setContext(valid);
52
                errors.push(error);
53
                break;
54
            }
55
        }
56
57
        if (errors.length > 0) {
58
            throw new ValidationError(this._cottus, errors);
59
        }
60
61
        return valid;
62
    }
63
}
64
65
export default class Cottus {
66
    constructor(conf = {}) {
67
        this.rules = {};
68
        this.addRules(conf.rules || []);
69
    }
70
71
    compile(schema) {
72
        const validator = new Validator(this, schema);
73
74
        validator.parse();
75
76
        return validator;
77
    }
78
79
    addRules(rules) {
80
        rules.forEach(rule => this.rules[rule.schema] = rule);
81
    }
82
83
    addRule(rule) {
84
        this.addRules([ rule ]);
85
    }
86
}
87